"""
@File         : route_depend.py
@Time         : 2024/05/08 16:38:59
@Author       : Qrj
@Email        : 923207736@qq.com
@Description  : 路由依赖(用于维持本业务的路由部分整洁)
"""

from typing import Annotated

from fastapi import Depends, Query
from sqlalchemy.ext.asyncio import AsyncSession

from src.business.admin.system.oauth2.token.dependencies.no_introduction import (
    GetTenantIdFromAccessTokenDependencies,
    GetUserNameFromAccessTokenDependencies,
)
from src.business.admin.system.station_message.record.schemas.validate import (
    StationMessageRecordCreateValidateSchema,
)
from src.business.admin.system.station_message.record.service import (
    StationMessageRecordService,
)
from src.business.admin.system.station_message.template.schemas.out import (
    StationMessageTemplateCompleteOutSchema,
)
from src.business.admin.system.station_message.template.schemas.validate import (
    StationMessageTemplateCreateValidateSchema,
    StationMessageTemplateDeleteValidateSchema,
    StationMessageTemplateListSearchValidateSchema,
    StationMessageTemplateUpdateStatusValidateSchema,
    StationMessageTemplateUpdateValidateSchema,
)
from src.business.admin.system.station_message.template.service import (
    StationMessageTemplateService,
)
from src.dependencies.no_introduction import depend_get_master_db_session
from src.schemas.validate import (
    OrderBySearchValidateSchema,
    PaginationSearchValidateSchema,
)


async def route_depend_get_station_message_template(
    station_message_template_id: Annotated[
        int, Annotated[int, Query(..., ge=1, description="站内信模板ID")]
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> StationMessageTemplateCompleteOutSchema:
    """查询单个站内信模板的路由依赖

    Args:
        station_message_template_id (Annotated[int, Depends): 站内信模板ID
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        StationMessageTemplateCompleteOutSchema: 站内信模板信息
    """
    station_message_template: StationMessageTemplateCompleteOutSchema = (
        await StationMessageTemplateService.get_station_message_template_use_id(
            db_session,
            station_message_template_id,
            raise_disabled_exception=False,
            raise_expired_exception=False,
        )
    )
    return station_message_template


async def route_depend_list_station_message_template(
    search_schema: StationMessageTemplateListSearchValidateSchema,
    pagination_schema: PaginationSearchValidateSchema,
    order_schema: OrderBySearchValidateSchema,
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> tuple[list[StationMessageTemplateCompleteOutSchema], int]:
    """查询站内信模板列表(无公告内容)的路由依赖

    Args:
        search_schema (Annotated[StationMessageTemplateListSearchValidateSchema, Depends): 查询参数
        pagination_schema (Annotated[PaginationSearchValidateSchema, Depends): 分页参数
        order_schema (Annotated[OrderBySearchValidateSchema, Depends): 排序参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池

    Returns:
        tuple[list[StationMessageTemplateNoContentOutSchema], int]: 查询结果
    """
    # 注解
    search_pagination_result: list[StationMessageTemplateCompleteOutSchema]
    search_total: int
    # 列表结果查询
    (
        search_pagination_result,
        search_total,
    ) = await StationMessageTemplateService.list_search_station_message_template(
        db_session, search_schema, pagination_schema, order_schema
    )
    return search_pagination_result, search_total


async def route_depend_create_station_message_template(
    create_schema: StationMessageTemplateCreateValidateSchema,
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
) -> int:
    """创建站内信模板的依赖

    Args:
        create_schema (StationMessageTemplateCreateValidateSchema): 创建所需的参数
        username: 当前token指向的站内信模板名
        db_session: 数据库连接池

    Returns:
        int: 创建的sql的主键ID
    """
    # 创建站内信模板
    created_id = await StationMessageTemplateService.create_station_message_template(
        db_session, create_schema, creator_username=username
    )
    return created_id


async def route_depend_update_station_message_template(
    update_schema: StationMessageTemplateUpdateValidateSchema,
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """修改站内信模板的路由依赖

    Args:
        update_schema (StationMessageTemplateUpdateValidateSchema): 修改所需的参数
        username: 当前token指向的站内信模板名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await StationMessageTemplateService.update_station_message_template(
        db_session, update_schema, updater_username=username
    )


async def route_depend_update_station_message_template_status(
    update_schema: StationMessageTemplateUpdateStatusValidateSchema,
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """单独修改站内信模板状态的路由依赖

    Args:
        update_schema (StationMessageTemplateUpdateStatusValidateSchema): 修改所需的参数
        username: 当前token指向的站内信模板名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 修改操作
    await StationMessageTemplateService.update_station_message_template_status(
        db_session, update_schema, updater_username=username
    )


async def route_depend_delete_station_message_template(
    delete_schema: StationMessageTemplateDeleteValidateSchema,
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """删除站内信模板的路由依赖

    Args:
        delete_schema (StationMessageTemplateDeleteValidateSchema): 删除所需的参数
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 删除操作
    await StationMessageTemplateService.delete_station_message_template(
        db_session, delete_schema
    )


async def route_depend_send_test_station_message(
    send_schema: StationMessageRecordCreateValidateSchema,
    tenant_id: Annotated[
        int, Depends(GetTenantIdFromAccessTokenDependencies.from_header)
    ],
    username: Annotated[
        str, Depends(GetUserNameFromAccessTokenDependencies.from_header)
    ],
    db_session: Annotated[AsyncSession, Depends(depend_get_master_db_session)],
):
    """发送站内信测试消息的路由依赖

    Args:
        send_schema (StationMessageRecordCreateValidateSchema): 发送所需参数
        tenant_id (Annotated[ int, Depends): 租户ID
        username (Annotated[ str, Depends): 当前token指向的站内信模板名
        db_session (Annotated[AsyncSession, Depends): 数据库连接池
    """
    # 发送操作
    await StationMessageRecordService.create_station_message_record(
        db_session, tenant_id, send_schema, creator_username=username
    )
